#include "configuration.h"

#include <cassert>

namespace jolfs
{

const Configuration::Node* Configuration::Volume::NO_NODE = 0;

//==============================================================================
// NODE
//==============================================================================

Configuration::Node::Node()
  : source_( ""          ),
    type_  ( Configuration::Node::LOCAL )
{
}

Configuration::Node::Node( const Configuration::Node& other )
  : source_( other.source_ ),
    type_  ( other.type_   )
{
}

Configuration::Node::Node( Configuration::Node::Type type, const std::string& source )
  : type_  ( type   ),
    source_( source )
{
}

Configuration::Node::~Node()
{
}

Configuration::Node::Type Configuration::Node::getType() const
{
  return type_;
}

const std::string& Configuration::Node::getSource() const
{
  return source_;
}

void Configuration::Node::setType( Configuration::Node::Type type )
{
  type_ = type;
}

void Configuration::Node::setSource( const std::string& source )
{
  source_ = source;
}

bool Configuration::Node::operator==( const Configuration::Node& other )
{
  return type_   == other.type_ &&
         source_ == other.source_;
}

//==============================================================================
// VOLUME
//==============================================================================

Configuration::Volume::Volume()
  : name_( ""    ),
    link_( MERGE ),
    node_( 0     )
{
}

Configuration::Volume::Volume( const Configuration::Volume& other )
  : name_( other.name_ ),
    link_( other.link_ ),
    node_( other.node_ )
{
}

Configuration::Volume::Volume( const std::string& name, Link link, const Node* node )
  : name_( name ),
    link_( link ),
    node_( node )
{
}

Configuration::Volume::~Volume()
{
}

const std::string& Configuration::Volume::getName() const
{
  return name_;
}

const Configuration::Volume::VolumeList& Configuration::Volume::getVolumes() const
{
  return volumes_;
}

Configuration::Volume::Link Configuration::Volume::getLink() const
{
  return link_;
}

const Configuration::Node* Configuration::Volume::getNode() const
{
  return node_;
}

void Configuration::Volume::setName( const std::string& name )
{
  name_ = name;
}

void Configuration::Volume::addVolume( Configuration::Volume* volume )
{
  volumes_.push_back( volume );
}

void Configuration::Volume::delVolume( Configuration::Volume* volume )
{
  VolumeList::iterator it = volumes_.begin();
  for ( ; it!=volumes_.end(); ++it )
    if ( *it == volume )
    {
      volumes_.erase(it);
      break;
    }
}

void Configuration::Volume::setLink( Configuration::Volume::Link link )
{
  link_ = link;
}

void Configuration::Volume::setNode( const Configuration::Node* node )
{
  node_ = node;
}

bool Configuration::Volume::operator==( const Configuration::Volume::Volume& other )
{
  return link_    == other.link_ &&
         node_    == other.node_ &&
         name_    == other.name_ &&
         volumes_ == other.volumes_;
}

//==============================================================================
// CONFIGURATION
//==============================================================================

Configuration::Configuration()
{
  mountVolume_.setName( "" );
  mountVolume_.setNode( Configuration::Volume::NO_NODE );
  mountVolume_.setLink( Configuration::Volume::MERGE );
}

Configuration::Configuration( const Configuration& other )
{
  //TODO
}

Configuration::~Configuration()
{
}

const Configuration::NodeList& Configuration::getNodes() const
{
  return nodes_;
}

const Configuration::Volume& Configuration::getMountVolume() const
{
  return mountVolume_;
}

Configuration::Volume& Configuration::getMountVolume()
{
  return mountVolume_;
}

void Configuration::addNode( Configuration::Node* node )
{
  nodes_.push_back( node );
}

void Configuration::delNode( const Configuration::Node* node )
{
  NodeList::iterator it = nodes_.begin();
  for (; it!=nodes_.end(); ++it )
    if ( *it==node )
    {
      nodes_.erase(it);
      break;
    }
}

} // namespace jolfs